home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / prdebug.c < prev    next >
C/C++ Source or Header  |  1991-12-19  |  2KB  |  109 lines

  1. /* prdebug.c */
  2. /* Functions that might help you to find a bug with a source-level debugger.
  3.  12/21/91 dump_stack renamed dump_ancestors
  4.  */
  5. #include <stdio.h>
  6. #ifndef MEGAMAX
  7. #include <assert.h>
  8. #endif
  9. #include "prtypes.h"
  10. #include "prlex.h"
  11.  
  12.  
  13. #ifndef NDEBUG
  14. #define CHK(X) if(!check_object(X))INTERNAL_ERROR("wild pointer");
  15. #endif 
  16.  
  17. /* Print the offset of a variable */
  18.  
  19. void pr_offset(nodeptr)
  20. node_ptr_t nodeptr;
  21. {
  22.     if(NODEPTR_TYPE(nodeptr)!= VAR)
  23.     {
  24.         pr_string("non var\n");
  25.     }
  26.     else
  27.         printf("%d",NODEPTR_OFFSET(nodeptr));
  28. }
  29.  
  30. /* print all open files */
  31. void pr_ofiles()
  32. {
  33.     extern struct named_ofile Open_ofiles[];
  34.     int i;
  35.     char *name;
  36.  
  37.     for(i = 0; i < MAXOPEN; i++)
  38.     {
  39.         name =    (Open_ofiles[i].o_filename);
  40.         if(*name){
  41.             tty_pr_string(name);
  42.             tty_pr_string("\n");
  43.         }
  44.     }
  45. }
  46.  
  47. void stack_dump()
  48. {
  49. extern dyn_ptr_t LastCframe; 
  50. if (LastCframe == NULL)
  51.    return;
  52. dump_ancestors( LastCframe );
  53. }
  54.  
  55. #ifdef HUNTBUGS
  56.  
  57. /* The function below was used to look for a bug 
  58.  *   - bughunt(__LINE__,__FILE__,..) was inserted in various parts of the program 
  59.  */
  60.  
  61. atom_ptr_t Mischievous, hash_search();
  62. extern atom_ptr_t Predicate;
  63. int Bug_hunt_flag = 0;  /* see do_builtin() in prlush */
  64. static unsigned long Sum;
  65.  
  66. unsigned long sum_bytes()
  67. {
  68. unsigned long result;
  69. char *p;
  70. char *start_watch_zone, *end_watch_zone;
  71.  
  72. start_watch_zone = (char *)hash_search("eq");/* last predicate in sprolog.ini */
  73. end_watch_zone = (char *)hash_search("log-session");/* last predicate in sprolog.ini */
  74. assert(start_watch_zone != NULL);
  75. assert(end_watch_zone != NULL);
  76. assert(start_watch_zone != end_watch_zone)
  77. for(result = 0L , p = start_watch_zone; p <= end_watch_zone;  p++)
  78.     result += *p;
  79. return(result);
  80. }
  81.  
  82. bughunt(line, file, msg)
  83. char *file,*msg;
  84. {
  85. static int first_time = 1;
  86.  
  87. if(Bug_hunt_flag == 0)return 1;
  88. if(first_time){
  89.     Sum = sum_bytes();
  90.     first_time = 0;
  91.      return(1);
  92.     }
  93.     else
  94.     if(Sum != sum_bytes())
  95.         {
  96.         printf("bughunt error %s line %d %s\n", file, line, msg);
  97.         getchar();
  98.         stack_dump();
  99.         Sum = sum_bytes();
  100.  
  101.         return(0);
  102.         }
  103. /* printf("Sum = %lu\n", Sum); */
  104.     return (1);
  105. }
  106.  
  107. #endif
  108.  
  109.